//
//  TextMessageView.m
//  BaseAPP
//
//  Created by Evisu on 17/7/13.
//  Copyright © 2017年 evisum. All rights reserved.
//
#define MAX_IMAGE_PIX 160

#import "TextMessageView.h"

@interface TextMessageView ()

//背景层
@property (nonatomic, strong)UIImageView *bgImageView;

//文字
@property (nonatomic, strong)UILabel *label;

//图片
@property (nonatomic, strong)UIImageView *imageView;

@end


@implementation TextMessageView

- (UIImageView *)bgImageView {
    
    if (!_bgImageView) {
        
        _bgImageView = [[UIImageView alloc] init];
    }
    
    return _bgImageView;
}

- (UILabel *)label {
    
    if (!_label) {
        
        _label = [[UILabel alloc] init];
        _label.font = [UIFont systemFontOfSize:14.0];
        _label.textColor = HEXColor(@"020203");
    }
    
    return _label;
}

- (UIImageView *)imageView {

    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    
    return _imageView;
}

- (void)initTextMessageViewWithContent:(NSString *)string isSender:(BOOL)sender {
    
    [self.imageView removeFromSuperview];
    
    [self.bgImageView sd_clearAutoLayoutSettings];
    
    //label赋值
    self.label.text = string;
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(190, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil];
    
    //设置背景图
    UIImage *bgImg = [UIImage imageNamed:@"message_receiver_background_normal"];//接收方背景图
    if (sender) {
        //发送方背景图
        bgImg = [UIImage imageNamed:@"message_sender_background_normal"];
    }
    bgImg = [bgImg resizableImageWithCapInsets:UIEdgeInsetsMake(bgImg.size.height*0.6, bgImg.size.width*0.5, bgImg.size.height*0.3, bgImg.size.width*0.5) resizingMode:UIImageResizingModeStretch];
    self.bgImageView.image = bgImg;
    
    [self addSubview:self.bgImageView];
    [self.bgImageView addSubview:self.label];
    
    self.bgImageView.sd_layout
    .topEqualToView(self)
    .leftEqualToView(self)
    .widthRatioToView(self,1)
    .heightRatioToView(self,1);
    
    self.label.sd_layout
    .centerXEqualToView(self.bgImageView)
    .centerYEqualToView(self.bgImageView)
    .widthIs(rect.size.width)
    .autoHeightRatio(0);
    
    self.sd_layout
    .centerXEqualToView(self.superview)
    .centerYEqualToView(self.superview)
    .widthIs(rect.size.width+40)
    .heightIs(rect.size.height+30);
}

- (void)initImageMessageViewWithImage:(UIImage *)image isSender:(BOOL)sender {

    [self.label removeFromSuperview];
    
    
    [self.bgImageView sd_clearAutoLayoutSettings];
    [self.bgImageView removeFromSuperview];
    
    
    //imageView赋值
    self.imageView.image = image;
    
    CGSize size = image.size;

    if (size.width > MAX_IMAGE_PIX || size.height > MAX_IMAGE_PIX) {
        
        CGFloat widhtRatio = MAX_IMAGE_PIX/size.width;
        CGFloat heightRatio = MAX_IMAGE_PIX/size.height;
        CGFloat scale = widhtRatio > heightRatio?heightRatio:widhtRatio;
        size = CGSizeMake(size.width*scale, size.height*scale);
    }
    
    //设置背景图
    UIImage *bgImg = [UIImage imageNamed:@"message_receiver_background_normal"];//接收方背景图
    if (sender) {
        //发送方背景图
        bgImg = [UIImage imageNamed:@"message_sender_background_normal"];
    }
    bgImg = [bgImg resizableImageWithCapInsets:UIEdgeInsetsMake(bgImg.size.height*0.6, bgImg.size.width*0.5, bgImg.size.height*0.3, bgImg.size.width*0.5) resizingMode:UIImageResizingModeStretch];
    self.bgImageView.image = bgImg;
    
    CALayer *layer = self.bgImageView.layer;
    layer.frame = CGRectMake(0, 0, self.imageView.width_sd, self.imageView.height_sd);
    
    self.imageView.layer.mask = layer;
    [self.imageView setNeedsDisplay];
    
    [self addSubview:self.imageView];
    
    self.bgImageView.sd_layout
    .topEqualToView(self)
    .leftEqualToView(self)
    .widthIs(size.width)
    .heightIs(size.height);
    
    self.imageView.sd_layout
    .leftEqualToView(self)
    .topEqualToView(self)
    .widthIs(size.width)
    .heightIs(size.height);
    
    self.sd_layout
    .centerXEqualToView(self.superview)
    .centerYEqualToView(self.superview)
    .widthIs(size.width)
    .heightIs(size.height);
    
}

@end
